/**
 * 
 */
package org.jinion.database.model.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinion.database.model.AbstractTable;
import org.jinion.exception.JinionRuntimeException;
import org.jinion.util.DataList;

/**
 * This class is the model of returned rows by query operation.
 * @author zhlmmc
 *
 */
public class TableRow {
	private static final Log log = LogFactory.getLog(TableRow.class);
	
	/**
	 * This row's number in the ResultSet.
	 */
	private int rowIndex;
	private Object[] values;
	private AbstractTable parentTable;
	
	public TableRow(AbstractTable parentTable){
		this.parentTable = parentTable;
		
		log.debug("New TableRow created.");
	}
	
	/**
	 * You have to setup columns first!
	 * @param rowNumber start from 1, 2...
	 * @param columnCount
	 */
	public TableRow(AbstractTable parentTable, int rowIndex){
		this(parentTable);
		this.rowIndex = rowIndex;
		values = new Object[parentTable.getColumns().size()];
		
		log.debug("New TableRow created, rowIndex: " + rowIndex);
	}
	
	/**
	 * This method will generate and return a map which stores the data of this row.<br>
	 * The column name is used as "key" and the value is used as "value". 
	 * @return
	 */
	public Map<String, Object> getMap(){
		Map<String, Object> map = new HashMap<String, Object>();
		DataList<TableColumn> list = parentTable.getColumns();
		int index = 0;
		for (Iterator<TableColumn> iterator = list.iterator(); iterator.hasNext();) {
			TableColumn column = iterator.next();
			map.put(column.getColumnName(), this.values[index++]);
		}
		
		return map;
	}
	
	/**
	 * 
	 * @param columnName
	 * @return
	 */
	public int getInt(String columnName){
		return this.getInt(this.parentTable.getColumnIndex(columnName));
	}
	
	/**
	 * 
	 * @param columnIndex
	 * @return
	 */
	public int getInt(int columnIndex){
		Object obj = this.values[columnIndex - 1];
		if (obj != null) {
			return Integer.parseInt(obj.toString());
		}
		else{
			throw new JinionRuntimeException("Can not convert NULL to int.");
		}
	}
	
	public String getString(String columnName){
		return this.getString(this.parentTable.getColumnIndex(columnName));
	}
	
	public String getString(int columnIndex){
		Object obj = this.values[columnIndex - 1];
		
		if (obj != null) {
			return obj.toString();
		}
		else{
			return null;
		}
	}
	
	public Object getValue(String columnName){	
		return this.getValue(this.parentTable.getColumnIndex(columnName));
	}
	
	public Object getValue(int columnIndex){
		if (columnIndex < 0) {
			return null;
		}
		
		return this.values[columnIndex - 1];
	}
	
	public void setValue(int columnIndex, Object value){
		this.values[columnIndex - 1] = value;
	}
	
	public int getRowNumber() {
		return rowIndex;
	}

	public Object[] getValues() {
		return values;
	}

	public void setValues(Object[] values) {
		this.values = values;
	}

	public int getRowIndex() {
		return rowIndex;
	}

	public AbstractTable getParentTable() {
		return parentTable;
	}
	
}
